package com.alan.libapi.business.dao;

import com.alan.libapi.api.ALTcpClient;
import com.alan.libapi.api.ApiResponse;
import com.alan.libapi.api.IALTcpClient;
import com.alan.libapi.api.Impl.ApiTcpImpl;
import com.alan.libapi.business.IBusinessTcp;
import com.alan.libapi.model.business.ConnectedDeviceInfo;
import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.libapi.model.business.TcpResponseStatus;

import java.io.IOException;
import java.util.List;

/**
 * @className: BusinessTcpImpl
 * @describtion: BusinessTcpImpl
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/28 19:28
 */
public class BusinessTcpImpl implements IBusinessTcp {


    private ApiTcpImpl apiTcpImpl;

    private static BusinessTcpImpl _instance;

    public static BusinessTcpImpl getInstance(){
        if(_instance == null){
            _instance = new BusinessTcpImpl();
        }
        return _instance;
    }

    private BusinessTcpImpl() {
        super();
    }

    @Override
    public void setALTcpClient(ALTcpClient alTcpClient) {
        if(apiTcpImpl != null){
            try {
                apiTcpImpl.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            apiTcpImpl = null;
        }
        apiTcpImpl = new ApiTcpImpl(alTcpClient);
    }

    @Override
    public void sendMasterInfo(final int deviceMasterId, CallbackListener<TcpResponseStatus> callbackListener) {
        if(apiTcpImpl == null){
            return;
        }
        BaseAsyncTaskThread<TcpResponseStatus> baseAsyncTaskThread = new BaseAsyncTaskThread<TcpResponseStatus>(callbackListener) {
            @Override
            protected ApiResponse<TcpResponseStatus> doInBackground() {
                return apiTcpImpl.sendMasterInfo(deviceMasterId);
            }
        };
        baseAsyncTaskThread.execute();
    }

    @Override
    public void sendWifiInfo(final int deviceMasterId, final List<SharedWifiInfo> wifiInfoList, CallbackListener<TcpResponseStatus> callbackListener) {
        if(apiTcpImpl == null){
            return;
        }
        BaseAsyncTaskThread<TcpResponseStatus> baseAsyncTaskThread = new BaseAsyncTaskThread<TcpResponseStatus>(callbackListener) {
            @Override
            protected ApiResponse<TcpResponseStatus> doInBackground() {
                return apiTcpImpl.sendWifiInfo(deviceMasterId, wifiInfoList);
            }
        };
        baseAsyncTaskThread.execute();
    }

    @Override
    public void modifyDeviceWifi(final int deviceMasterId, final String SSID, final String password, CallbackListener<TcpResponseStatus> callbackListener) {
        if(apiTcpImpl == null){
            return;
        }
        BaseAsyncTaskThread<TcpResponseStatus> baseAsyncTaskThread = new BaseAsyncTaskThread<TcpResponseStatus>(callbackListener) {
            @Override
            protected ApiResponse<TcpResponseStatus> doInBackground() {
                return apiTcpImpl.modifyDeviceWifi(deviceMasterId, SSID, password);
            }
        };
        baseAsyncTaskThread.execute();
    }

    @Override
    public void getDeviceUserList(final int deviceMasterId, CallbackListener<List<ConnectedDeviceInfo>> callbackListener) {
        if(apiTcpImpl == null){
            return;
        }
        BaseAsyncTaskThread<List<ConnectedDeviceInfo>> baseAsyncTaskThread = new BaseAsyncTaskThread<List<ConnectedDeviceInfo>>(callbackListener) {
            @Override
            protected ApiResponse<List<ConnectedDeviceInfo>> doInBackground() {
                return apiTcpImpl.getDeviceUserList(deviceMasterId);
            }
        };
        baseAsyncTaskThread.execute();
    }

    @Override
    public void deleteDeviceUser(final int deviceMasterId, final String userMac, CallbackListener<TcpResponseStatus> callbackListener) {
        if(apiTcpImpl == null){
            return;
        }
        BaseAsyncTaskThread<TcpResponseStatus> baseAsyncTaskThread = new BaseAsyncTaskThread<TcpResponseStatus>(callbackListener) {
            @Override
            protected ApiResponse<TcpResponseStatus> doInBackground() {
                return apiTcpImpl.deleteDeviceUser(deviceMasterId, userMac);
            }
        };
        baseAsyncTaskThread.execute();
    }

    @Override
    public boolean isAlive() {
        if(apiTcpImpl == null){
            return false;
        }
        return apiTcpImpl.isAlive();
    }

    @Override
    public void retryConnection() throws IOException {
        if(apiTcpImpl == null){
            return;
        }
        apiTcpImpl.retryConnection();
    }

    @Override
    public void close() throws IOException {
        if(apiTcpImpl == null){
            return;
        }
        apiTcpImpl.close();
    }
}
